<?php
class Contents extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index($category_id)
	{
		if($_POST)
		{
			$category = new Category((!empty($_POST['id']))?$_POST['id']:NULL);
			if(!empty($_POST['id']))unset($_POST['id']);
			$_POST['name'] = lang_encode($_POST['name']);
			$category->from_array($_POST);
			$category->save();
			set_notify('success', lang('save_data_complete'));
			redirect('contents/admin/contents/index/'.$_POST['parent_id']);
		}
		$data['category'] = new category($category_id);
		if(($data['category']->structure->type=='list')||($data['category']->structure->type=='download'))
		{
			$data['contents'] = new content();
			$data['contents']->where("category_id = ".$category_id);
			if(@$_GET['search'])$data['contents']->where("title like '%".$_GET['search']."%'");
			$data['contents']->order_by('id','desc')->get_page(10);
			$this->template->append_metadata(js_checkbox('approve'));
			$this->template->build('admin/content_index',$data);
		}
		elseif($data['category']->structure->type=='category'|$data['category']->structure->type=='agency')
		{
			if(!empty($_GET['edit']))
			{
				$data['edit'] = new Category($_GET['edit']);
			}
			$data['categories'] = new Category();
			$data['categories']->where('parent_id',$category_id)->get();
			$data['parent'] = new category($category_id);
			$this->template->build('admin/content_category',$data);
		}
		elseif($data['category']->structure->type=='contact')
		{
			$data['content'] = new content();
			$data['content']->where('category_id',$category_id)->get();
			$this->template->build('admin/content_contact',$data);
		}
		elseif($data['category']->structure->type=='page')
		{
			$data['content'] = new content();
			$data['content']->where('category_id',$category_id)->get();
			$this->template->build('admin/content_page',$data);
		}
		
	}
	
	function form($category_id,$id=FALSE)
	{
		$data['category'] = new category($category_id);
		$data['content'] = new content($id);
		$this->template->append_metadata(js_datepicker());
		if($data['category']->type=='download')
		{
			$this->template->build('admin/content_download',$data);
		}
		else
		{
			$this->template->append_metadata(js_lightbox());
			$this->template->build('admin/content_form',$data);
		}
		
	}
	
	function preview()
	{
		$data['slug'] = 'main';
		$this->template->set_theme('default');
		$this->template->set_layout('layout');
		$this->template->build('admin/content_preview',$data);
	}
	function save($id=FALSE)
	{
		if($_POST)
		{
			$content = new content($id);	
			$category = new Category($_POST['category_id']);
			if(isset($_POST['start_date']))$_POST['start_date'] = Date2DB($_POST['start_date']);
			if(isset($_POST['end_date']))$_POST['end_date'] = Date2DB($_POST['end_date']);
			if(!$id)$_POST['user_id'] = $this->session->userdata('id');
			if(@$_FILES['image']['name'])
			{	
				if($id)
				{
					if($category->layout->thumbnail)
					{
						$content->delete_file('uploads/content/thumbnail','image');
					}
					
					if($category->layout->small)
					{
						$content->delete_file('uploads/content/thumbnail/small','image');
					}
				}
				
				if($category->layout->thumbnail)
				{
					list($width,$height) = explode('x',$category->layout->thumbnail);
					$content->image = $content->upload($_FILES['image'],'uploads/content/thumbnail',$width,$height);
					if($category->layout->small)
					{
						list($width,$height) = explode('x',$category->layout->small);
						$content->thumb('uploads/content/thumbnail/small',$width,$height);
					}
				}
			}
			if(@$_FILES['file']['name'])
			{
				if($id)$content->delete_file('uploads/content/download','file');
				$content->file = $content->upload($_FILES['file'],'uploads/content/download');
			}
			$content->from_array($_POST);
			$content->save();
			set_notify('success', lang('save_data_complete'));
			redirect('contents/admin/contents/index/'.$content->category_id);
		}
		else
		{
			redirect($_SERVER['HTTP_REFERER']);
		}
		
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$content = new content($id);
			$_POST['approve_id'] = $this->session->userdata('id');
			$content->approve_date = date("Y-m-d H:i:s");
			$content->from_array($_POST);
			$content->save();
		}

	}
	
	function delete($id=FALSE)
	{
		if($id)
		{
			$content = new Content($id);
			$content->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function download($id)
	{
		$content = new Content($id);
		$this->load->helper('download');
		$data = file_get_contents("uploads/content/download/".basename($content->file));
		$name = basename($content->file);
		force_download($name, $data); 
	}
	
	function delete_file($id)
	{
		$content = new Content($id);
		$content->delete_file('uploads/content/download','file');
		$content->file = '';
		$content->save();
		set_notify('success', 'ลบไฟล์เรียบร้อยแล้วค่ะ');
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function remove_image()
	{
		$user = new User($this->session->userdata('id'));
		$user->profile->delete_file($user->profile->id,'uploads/users/','avatar');
		$user->profile->avatar = NULL;
		$user->profile->save();
		set_notify('success', lang('remove_image_complete'));
		redirect('users/admin/profiles');	
	}
	
	public function ddl($name)
	{
		if($_POST)
		{
			$model = str_replace('_id', '', $name);
			$ddl = new $model;
			if(!empty($_POST['id']))
			{
				$ddl->get_by_id($_POST['id']);
				unset($_POST['id']);
			}
			$ddl->from_array($_POST);
			$ddl->save();
			echo form_dropdown($name,get_option('id','name',plural($model),' order by name'),$ddl->id);
		}
	}
	
	public function ddl_delete($name)
	{
		$model = str_replace('_id', '', $name);
		$ddl = new $model;
		$ddl->get_by_id($_POST['id']);
		$ddl->delete();
		echo form_dropdown($name,get_option('id','name',plural($model),' order by name'));

	}
	
	

}
?>